/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.promo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.promo.Promo;
import utility.NameValueList;
import utility.StringUtils;
import utility.typeSafe.EnumUtils;
import utility.typeSafe.PromoJenis;

/**
 *
 * @author jay
 */
public class PromoTM extends AbstractTableModel {
    
  //  private static final long serialVersionUID = 1L;\
  
    private List<Promo> data;
    private List<NameValueList> promoJenis = EnumUtils.enumToNameValueList(PromoJenis.class);

    public PromoTM(List<Promo> data) {
        this.data = data;
    }

    public PromoTM() {
        this(new ArrayList<Promo>(0));
    }

    public Promo get(int index) {
        return data.get(index);
    }

    public void add(Promo var) {
        data.add(var);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, Promo var) {
        data.set(index, var);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<Promo> vars) {
        data.addAll(vars);
        fireTableRowsInserted(getRowCount() - (vars.size()), getRowCount() - 1);
    }
         
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 9;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: return rowIndex+1;
            case 1: return data.get(rowIndex).getPromoName();  
            case 2: 
                if (data.get(rowIndex).getStartDate()!=null){
                    return StringUtils.formatDate(data.get(rowIndex).getStartDate());
                } return null;
			case 3: 
                if (data.get(rowIndex).getEndDate()!=null){
                    return StringUtils.formatDate(data.get(rowIndex).getEndDate());
                } return null;
			
            case 4: if(data.get(rowIndex).getJenis() != null) {
                        int i = NameValueList.indexOf(promoJenis, data.get(rowIndex).getJenis());
                        return promoJenis.get(i).getValue();
                    } else {return null;}
                
            case 5: return data.get(rowIndex).getNotes();  
            case 6: return data.get(rowIndex).getWhois();  
                
            case 7: return data.get(rowIndex).getAktive();
                        
            default: return null;
        }
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "NO";
            case 1: return "NAMA";
            case 2: return "TGL MULAI";
            case 3: return "TGL AKHIR";
            case 4: return "JENIS";
            case 5: return "BERLAKU UNTUK";
            case 6: return "KETERANGAN";
            case 7: return "[V]";
            case 8: return "";
            default: return null;
        }
    }
    
    @Override
    public Class getColumnClass(int column) {
      //  return getValueAt(0, column).getClass();
        
        switch (column) {
            case 0: return int.class;
            case 7: return Boolean.class;
            default: return String.class;
        }    
        
    }
    
}
